package com.chattingroom.mqproducer.mqproducer.feign;

import com.chattingroom.mqcosumer.feign.interfaces.MyConsumerFeign;
import com.chattingroom.mqproducer.mqproducer.feign.interfaces.MyProducerFeign;
import com.rabbitmq.ChattingRoom.util.common.enums.FixedQueueEnum;
import com.rabbitmq.ChattingRoom.util.common.req.SendMessageToQueueReq;
import com.rabbitmq.ChattingRoom.util.common.req.TellUserQuitChattingRoomReq;
import com.rabbitmq.ChattingRoom.util.utils.JSONUtil;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Auther: zhouxw
 * @Date: 2019/11/25 21:03
 * @company：CTTIC
 */
@RestController
public class ProducerFeignController implements MyProducerFeign {

    @Autowired
    private RabbitTemplate rabbit;
    @Autowired
    private RabbitAdmin rabbitAdmin;
    @Autowired
    private MyConsumerFeign myConsumerFeign;

    @Override
    public boolean createQueue(String queueName) {
        Queue queue = new Queue(queueName, true, false, false, null);
        FanoutExchange fanoutExchange = new FanoutExchange("test.shtl.fanout2");
        rabbitAdmin.declareQueue(queue);
        rabbitAdmin.declareExchange(fanoutExchange);
        rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(fanoutExchange));
        String reqStr = myConsumerFeign.listener(queueName);
        if (!reqStr.equals("添加完成")) {
            return false;
        }
        return true;
    }

    @Override
    public boolean sendMessageToQueue(SendMessageToQueueReq req) {
        rabbit.convertAndSend(req.getQueueName(),JSONUtil.stringify(req.getMessage()));
        System.err.println("发送完成");
        return true;
    }

    @Override
    public boolean sendQuitMessageToQueue(TellUserQuitChattingRoomReq req) {
        rabbit.convertAndSend(FixedQueueEnum.TELLQUITCHATTINGROOM.getQueueName(),JSONUtil.stringify(req));
        System.err.println("发送完成");
        return true;
    }

    @Override
    public boolean deleteByQueueName(String queueName) {
        return rabbitAdmin.deleteQueue(queueName);
    }

}
